#include <iostream>
#include <fstream>
#include <string>
#include "contacts.pb.h"

void AddContact(contacts::PeopleInfo *con)
{
    std::cout << "---------------新增联系人---------------" << std::endl;
    std::cout << "输入联系人姓名: ";
    std::string name;
    std::getline(std::cin, name);
    con->set_name(name);
    int age = 0;
    std::cout << "输入联系人年龄: ";
    std::cin >> age;
    con->set_age(age);
    // 清空cin缓冲区
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    // 新增电话
    for (int i = 1; true; i++)
    {
        std::cout << "---请输入该联系人的电话" << i << "(只输入空格表示完成)" << ": ";
        std::string number;
        std::getline(std::cin, number);
        if (number.size() == 0)
            break;
        contacts::PeopleInfo_Phone *phone = con->add_phones();
        // phone->set_phone(number);
        phone->set_number(number);
        std::cout << "--请输入该联系人电话类型:|1.移动电话--2.固定电话|-----: ";
        int type;
        std::cin >> type;
        std::cin.ignore(256, '\n');
        switch (type)
        {
        case 1:
            phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
            break;
        case 2:
            phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
            break;
        }
        // 填充地址信息
        contacts::Address address;
        std::cout << "请输入工作单位地址(不填请按空格结束): ";
        std::string util_address;
        std::getline(std::cin, util_address);
        address.set_util_address(util_address);
        std::string home_address;
        std::cout << "请输入家庭地址(不填请按空格结束): ";
        std::getline(std::cin, home_address);
        address.set_home_address(home_address);
        // people中的mutable_data方法会返回一个开辟好的any对象地址
        google::protobuf::Any *any = con->mutable_data();
        // 将message信息解析到any中
        any->PackFrom(address);
        // 添加其他联系方式
        std::cout << "请输入其他联系方式->|1.qq---2.微信|: ";
        int op = 0;
        std::cin >> op;
        std::cin.ignore(256, '\n');
        std::string other_contact;
        switch (op)
        {
        case 1:
            std::cout << "请输入qq号: ";
            std::getline(std::cin, other_contact);
            con->set_qq(other_contact);
            break;
        case 2:
            std::cout << "请输入微信号: ";
            std::getline(std::cin, other_contact);
            con->set_wechat(other_contact);
            break;
        default:
            std::cout<<"未知错误!"<<std::endl;
            break;
        }
        std::cout<<"请输入该联系人的备注信息: ";
        google::protobuf::Map<std::string, std::string>* remark = con->mutable_remarks();
        for(int i = 1;;i++)
        {
            std::cout<<"请输入备注标题"<<i<<"(按下空格结束): ";
            std::string key;
            std::getline(std::cin,key);
            if(key.size() == 0) break;
            std::cout<<"请输入备注内容"<<"(按下空格结束): ";
            std::string val;
            std::getline(std::cin,val);
            if(val.size() == 0) break;
            remark->insert({key,val});
        }
    }
    std::cout << "---------------添加联系人成功---------------" << std::endl;
}
int main()
{
    contacts::Contacts cons;
    std::ifstream in("contacts.bin", std::ios::binary);
    if (!in.is_open())
    {
        std::cout << "contacts.bin is not exist,create new contacts.bin" << std::endl;
    }
    else if (!cons.ParseFromIstream(&in))
    {
        std::cout << "Parse contact info error!" << std::endl;
        in.close();
        return -1;
    }

    AddContact(cons.add_contacts());

    // 序列化写入文件
    std::ofstream out("contacts.bin", std::ios::trunc | std::ios::binary);
    if (!out.is_open())
    {
        std::cout << "write file error!" << std::endl;
        return -2;
    }
    if (!cons.SerializePartialToOstream(&out))
    {
        std::cout << "序列化失败!" << std::endl;
        return -3;
    }

    in.close();
    out.close();
    return 0;
}